const app = getApp();
Page({
  data: {
    send_disabled:false,
    disabled:false,
    time:'获取验证码',
    mobile:'18871452895', //输入的电话
    currentTime:60,
  },

  onShow:function(){
    let _this = this;
    _this.getUserInfo(); //获取账本
  },

  //获取个人信息
  getUserInfo:function(){
    let _this = this;
    wx.showLoading({
      title: '加载中',
    })
    app._post('api/member/getUserInfo', {}, function(result) {
      wx.hideLoading({})
      if(result.code == 200){
        _this.setData({
          is_login:true,
        })
      }else{
        wx.navigateTo({
          url: '../auth/auth'
        })
      }
    });
  },

  //倒计时60秒
  getCode: function () {
    var that = this;
    var currentTime = that.data.currentTime;
      that.setData({
      time: currentTime + '秒',
      send_disabled:true
    })
    var interval = setInterval(function () {
      that.setData({
        time: (currentTime - 1) + '秒'
      })
      currentTime--;
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新获取',
          currentTime: 60,
          send_disabled: false
        })
      }
    }, 1000)
  },

  //监听电话输入框
  input_phone:function(e){
    let _this = this,
     mobile = _this.data.mobile;
    _this.setData({
      mobile:e.detail.value
    })
  },

  send_msg:function(){
    let _this = this,
     mobile = _this.data.mobile;

    if(mobile == ''){
      wx.showToast({
        title: '手机号码不能为空',
        icon: 'none',
        duration: 2000
      })
    }else{
      let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
      if (!reg.test(mobile)) {
        this.data.error = '手机号不符合要求';
        return false;
      }
      _this.getCode();
      app._post('api/member/send_msg', {scene:'lock_mobile',mobile:mobile}, function(result) {
        wx.showToast({
          title: result.msg,
          icon: 'none',
          duration: 2000
        })
      });
    }
  },

  //提交
  formSubmit:function(e){
    let _this = this,
      values = e.detail.value,
      yzm = values.yzm,
      mobile = _this.data.mobile;//短信验证

    _this.setData({
      disabled:true
    })

    app._post('api/member/mobile_lock_check', {mobile:mobile,yzm:yzm}, function(result) {
      if(result.code == 200){
        var icon = 'success';
        setTimeout(function () {
          wx.navigateBack({})
        }, 1500);
      }else{
        var icon = 'none';
        _this.setData({
          disabled:false
        })
      }
      wx.showToast({
        title: result.msg,
        icon: 'none',
        duration: 2000
      })

    });
  },
})